using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.Office.Server.Search;
using Microsoft.Office.Server.Search.Administration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Xml.Serialization;

namespace IWCommunity.WebControls
{
    internal class ScopesHelper
    {

        //default constructor
        public ScopesHelper()
        {
        }

        //static method to get our display group, as well as all of the search
        //scopes that they contain
        public static DisplayGroupData InitScopes(string DisplayGroup)
        {
            DisplayGroupData ret = null;

            //get the scope info
            SPSecurity.RunWithElevatedPrivileges(
                delegate { ret = GetDefaultScope(DisplayGroup); });

            return ret;
        }

        private static DisplayGroupData GetDefaultScope(string DisplayGroup)
        {
            //figure out the default scope in here using the 
            DisplayGroupData ret = new DisplayGroupData();
            string ourScope = string.Empty;
            int ourScopeID = 0;
            int ourScopeSequence = 0;
            ArrayList ourScopes = new ArrayList();

            const string SCOPE_KEY = "srchDefaultScope";
            const string SCOPE_ID_KEY = "srchDefaultScopeID";
            const string SCOPE_SEQUENCE_KEY = "srchDefaultScopeSequence";
            const string SCOPES_KEY = "srchScopeCollection";

            try
            {
                //look for the value in cache
                if (HttpContext.Current.Cache[SCOPE_KEY] != null)
                {
                    ourScope = (string)HttpContext.Current.Cache[SCOPE_KEY];
                    ourScopeID = (int)HttpContext.Current.Cache[SCOPE_ID_KEY];
                    ourScopeSequence = (int)HttpContext.Current.Cache[SCOPE_SEQUENCE_KEY];
                    ourScopes = (ArrayList)HttpContext.Current.Cache[SCOPES_KEY];
                }
                else
                {
                    //item isn't in cache so we have to get it from the 
                    //DisplayGroup for the site, using the default scope property

                    //get the current search scope
                    SearchContext ctx = null;

                    //get the current site, and get the search context from it
                    //using (SPSite curSite = SPControl.GetContextSite(HttpContext.Current))
                    //  {
                    SPSite curSite = SPContext.Current.Site;
                    //get the searchcontext from the site
                    ctx = SearchContext.GetContext(curSite);

                    //create an instance of the Scopes class
                    Scopes curScopes = new Scopes(ctx);

                    //get the scope group
                    ScopeDisplayGroup ourGroup =
                        curScopes.GetDisplayGroup(new Uri(curSite.Url), DisplayGroup);

                    //see if we found something
                    if (ourGroup != null)
                    {
                        //get the default scope for our display group
                        ourScope = ourGroup.Default.Name;
                        ourScopeID = ourGroup.Default.ID;

                        //store it in  cache for next time
                        HttpContext.Current.Cache.Insert(SCOPE_KEY, ourScope, null,
                            System.DateTime.Now.AddHours(4), TimeSpan.Zero);
                        HttpContext.Current.Cache.Insert(SCOPE_ID_KEY, ourScopeID, null,
                            System.DateTime.Now.AddHours(4), TimeSpan.Zero);

                        //get the scopes for our display group
                        for (int i = 0; i < ourGroup.Count; i++)
                        {
                            //add it in the order in which it is returned
                            ourScopes.Add(ourGroup[i]);

                            //see if this is our default scope
                            if (ourGroup[i].Name == ourScope) ourScopeSequence = i;
                        }

                        //put scope sequence and scopes in cache
                        HttpContext.Current.Cache.Insert(SCOPE_SEQUENCE_KEY, ourScopeSequence,
                             null, System.DateTime.Now.AddHours(4), TimeSpan.Zero);
                        HttpContext.Current.Cache.Insert(SCOPES_KEY, ourScopes, null,
                            System.DateTime.Now.AddHours(4), TimeSpan.Zero);

                        //dispose resources associated with OwningSite object
                        // ourGroup.OwningSite.Dispose();
                        //}
                    }

                    //if (curSite != null)
                    //    curSite.Dispose(); //this seemed to keep thowring exception on post back....
                }

                //set the scope properties so we can use it in our query
                ret.DefaultScopeName = ourScope;
                ret.DefaultScopeID = ourScopeID;
                ret.DefaultScopeSequence = ourScopeSequence;
                ret.AllScopes = ourScopes;
            }
            catch (Exception ex)
            {
                //just toss out the error for now
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
            
            return ret;
        }
    }


    public class DisplayGroupData
    {
        public string DefaultScopeName = string.Empty;
        public int DefaultScopeID = 0;
        public int DefaultScopeSequence = 0;
        public System.Collections.ArrayList AllScopes;

        //default constructor
        public DisplayGroupData()
        {
        }
    }
}
